package com.huatai.manage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.huatai.common.util.StringUtil;
import com.huatai.manage.common.constant.ComConstant;
import com.huatai.manage.entity.TbCodeMainEntity;
import com.huatai.manage.entity.TbCodeManageEntity;
import com.huatai.manage.mapper.TbCodeMainMapper;
import com.huatai.manage.query.PageQuery;
import com.huatai.manage.service.TbCodeMainService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huatai.manage.service.TbCodeManageService;
import com.huatai.manage.utils.ComUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 主编码; 服务实现类
 * </p>
 *
 * @author luoxc
 * @since 2023-07-05
 */
@Service
public class TbCodeMainServiceImpl extends ServiceImpl<TbCodeMainMapper, TbCodeMainEntity> implements TbCodeMainService {

	@Autowired
	private TbCodeManageService manageService;

	@Autowired
	private TbCodeMainMapper mapper;

	@Override
	public boolean removes(List<String> codeMainIds) {
		List<String> codeMains = getCodeMains(codeMainIds);
		//指定的主编码：模型类型、特效类型不能删除,子表在引用时不能删除
		if (ComUtil.listIsEmpty(codeMains)
			|| ComUtil.listIsNotEmpty(ComConstant.CODE_MAIN_DICTIONARY.stream().filter(codeMains::contains).collect(Collectors.toList()))
			|| ComUtil.listIsNotEmpty(manageService.listByCodeMains(codeMains))) return false;
		LambdaQueryWrapper<TbCodeMainEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.in(TbCodeMainEntity::getCodeMainId, codeMainIds);
		return remove(wrapper);
	}

	@Override
	public boolean update(TbCodeMainEntity tbCodeMainEntity) {
		LambdaUpdateWrapper<TbCodeMainEntity> wrapper = Wrappers.lambdaUpdate();
		wrapper.eq(TbCodeMainEntity::getCodeMainId, tbCodeMainEntity.getCodeMainId());
		return this.update(tbCodeMainEntity, wrapper);
	}

	@Override
	public TbCodeMainEntity getByCodeMainId(String codeMainId) {
		return getOne(Wrappers.<TbCodeMainEntity>lambdaQuery().eq(TbCodeMainEntity::getCodeMainId, codeMainId));
	}

	@Override
	public Page<TbCodeMainEntity> pageList(PageQuery<TbCodeMainEntity> pageQuery) {
		Page<TbCodeMainEntity> page = new Page<>(pageQuery.getCurrent(), pageQuery.getSize());
		TbCodeMainEntity data = pageQuery.getData();
		LambdaQueryWrapper<TbCodeMainEntity> wrapper = Wrappers.lambdaQuery();
		wrapper.like(StringUtil.isNotBlank(data.getCodeMainName()), TbCodeMainEntity::getCodeMainName, data.getCodeMainName());
		return this.page(page, wrapper);
	}

	/**
	 * 判断是否能删除
	 *
	 * @param codeMainIds
	 * @return
	 */
	public List<String> getCodeMains(List<String> codeMainIds) {
		QueryWrapper<TbCodeMainEntity> wrapper = new QueryWrapper<>();
		wrapper.select("code_main").lambda().in(TbCodeMainEntity::getCodeMainId, codeMainIds);
		return mapper.getAllByCodeMains(wrapper.lambda());

		/*if (ComUtil.listIsEmpty(codeMains)) return false;
		return ComUtil.listIsEmpty(codeMains.stream().filter(ComConstant.CODE_MAIN_DICTIONARY::contains).collect(Collectors.toList()));*/
	}

}
